package org.geektimes.rest.demo;

import org.junit.Test;

import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.core.Form;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

public class RestClientDemo {


    @Test
    public void testGet() {
        Client client = ClientBuilder.newClient();
        Response response = client
                .target("http://localhost:8080/test/foo")      // WebTarget
                .request() // Invocation.Builder
                .get();                                     //  Response

        String content = response.readEntity(String.class);

        System.out.printf("content = %s", content);
    }

    @Test
    public void testPostWithBody() {
        Client client = ClientBuilder.newClient();
        Entity<User> userEntity = Entity.json(new User(1, "小马哥"));
        Response response = client
                .target("http://localhost:8080/test/bar")      // WebTarget
                .request() // Invocation.Builder
                .post(userEntity);                                     //  Response

        String content = response.readEntity(String.class);

        System.out.printf("content = %s", content);
    }

    @Test
    public void testPostWithForm() {
        Client client = ClientBuilder.newClient();
        Entity<Form> formEntity = Entity.form(new Form("bar", "bar"));
        Response response = client
                .target("http://localhost:8080/test/bar2")      // WebTarget
                .request() // Invocation.Builder
                .post(formEntity);                                     //  Response

        String content = response.readEntity(String.class);

        System.out.printf("content = %s\n", content);
    }

}
